package day02;

import util.JdbcUtil;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;

public class TestBlob {


    public static void main(String[] args) {

        // insert();

        select();
    }


    public static void insert(){
        Connection con=null;
        PreparedStatement ps=null;
        InputStream in=null;
        try{
            con=JdbcUtil.getConnection();

            con.setAutoCommit(false);

            in=TestBlob.class
                       .getClassLoader()
                       .getResourceAsStream("day02/a.jpg");


            String sql=new StringBuffer()
                    .append("insert into t_blob ")
                    .append("    (file_path,file) ")
                    .append("values ")
                    .append("    (?,?) ")
                    .toString();

            ps=con.prepareStatement(sql);

            ps.setString(1,"C:\\Users\\Teacher\\Desktop\\jdbc\\src\\day02\\a.jpg");

            ps.setBinaryStream(2,in);

            ps.executeUpdate();

            con.commit();

        }catch (Exception e){
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }finally{
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            JdbcUtil.close(con,ps,null);
        }
    }

    public static void select(){
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;

        InputStream in=null;

        FileOutputStream out=null;
        try{
            con=JdbcUtil.getConnection();

            con.setAutoCommit(false);

            String sql=new StringBuffer()
                    .append("select * ")
                    .append("from t_blob ")
                    .append("where id=? ")
                    .toString();

            ps=con.prepareStatement(sql);

            ps.setInt(1,1);

            rs=ps.executeQuery();

            if(rs.next()){
                String filePath=rs.getString("file_path");

                System.out.println("filePath = " + filePath);

                Blob blob=rs.getBlob("file");

                in=blob.getBinaryStream();

                out=new FileOutputStream("d:/a.jpg");

                byte[] b=new byte[2048];

                int n;

                while((n=in.read(b))!=-1){
                    out.write(b,0,n);
                }
            }

            con.commit();

        }catch (Exception e){
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }finally{
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            JdbcUtil.close(con,ps,rs);
        }
    }



}
